package com.eportal.action.background;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.CBuilding;
import com.eportal.domain.Sysopt;
import com.eportal.service.CBuildingService;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author chencheng
 */
public class CBuildingAction extends BaseAction implements ModelDriven<CBuilding>{

	/**
	 * @author chencheng
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, Object> result = new HashMap<String, Object>();
	
	private CBuilding cBuilding = new CBuilding();
	@Autowired
    private CBuildingService cBuildingService;
	/**
     * 楼栋管理
     */
    public String listView() {
        return SUCCESS;
    }
    
    /**
     * 楼栋列表
     */
    public void list() throws Exception {
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	String communityId = getRequest().getParameter("communityId");
  	 	if (StringUtils.isBlank(communityId)) {
  	 		communityId = user.getCommunityId();
  		}
    	String where = sqlWhere(communityId);
    	List<CBuilding> CBuildings = cBuildingService.list(where);
    	responseBody(CBuildings);
    }

    private String sqlWhere(String communityId) {
    	StringBuilder where = new StringBuilder(" where 1=1 ");
		where.append(" and communityId ='");
		where.append(communityId);
		where.append("'");
		return where.toString();
	}

    /**
     * 楼栋新增
     */
    public String addView() {
    	getRequest().setAttribute("communityId", getRequest().getParameter("communityId"));
        return SUCCESS;
    }
    
    /**
     * 楼栋新增保存
     */
    public void add() throws IOException {
    	int building1 = Integer.parseInt(getRequest().getParameter("building1"));
    	int building2 = Integer.parseInt(getRequest().getParameter("building2"));
    	String preBuilding = getRequest().getParameter("preBuilding");
    	int unit = Integer.parseInt(getRequest().getParameter("unit"));
    	String preUnit = getRequest().getParameter("preUnit");
    	int floor = Integer.parseInt(getRequest().getParameter("floor"));
    	int room = Integer.parseInt(getRequest().getParameter("room"));
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	String communityId = getRequest().getParameter("communityId");
   	 	if (StringUtils.isBlank(communityId)) {
   	 		communityId = user.getCommunityId();
		}
    	result = cBuildingService.addMethod(building1,building2,preBuilding,unit,preUnit,floor,room,communityId);
        responseBody(result);
    }
    
	@Override
    public CBuilding getModel() {
        return cBuilding;
    }

}
